Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added export method to export highchart in different formats #94

Merged
merged 4 commits into from Jul 14, 2018

Conversation

Prakriti-nith
Copy link
Contributor

Added the methods export_image and export_image_iruby to export the Highchart in four formats: pdf, png, jpg and svg with the default format being pdf. This is working fine in rails app but I can't understand why it is not working in IRuby notebook. Still searching some way to make it work. I will add the example of rails app to demonstrate this feature.

Example:
hchart = Daru::View::Plot.new(data, options)
hchart.export_image('svg')

This example will first generate the chart and then download it in svg format like this:
image

@Prakriti-nith
Copy link
Contributor Author

I was using JQuery to get the Highchart object and then download it. For this, I was using jquery.min.js dependency. This dependency was not properly loaded in IRuby notebook (maybe conflicting with the existing dependencies of the jupyter notebook) and was creating problems. I have replaced the code to get the instance of HighCharts' object with the pure js code. It is now working fine in both IRuby notebook as well as in web frameworks.

@Prakriti-nith
Copy link
Contributor Author

Now, user along with format of the chart (in which it has to be exported) can provide the file name.
For example,

hchart = Daru::View::Plot.new(data, options)
hchart.export_image('daru', 'png')

This will download the chart as: daru.png

when 'svg'
"\n type: 'image/svg+xml',"
else
raise 'Invalid format'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's type error right?

def extract_export_code_iruby(placeholder=random_canvas_id, file_name='chart', type='pdf')
js = ''
js << "\n <script>"
js << "\n (function() {"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check alignments of the lines are correct in web page.

@@ -64,6 +64,16 @@ def export_html_file(plot, path='./plot.html')
File.write(path, str)
end

# @see #Daru::View::Plot.export_image
def export_image(plot, file_name='chart', type='pdf')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't we can find, if user running in IRuby notebook or console or web app?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, first I thought of creating only the export_image method. I tried to know whether the user is running in web app or IRuby notebook but couldn't do it. So, I created two different methods export_image and export_image_iruby instead

end

def export_image_iruby(plot, file_name, type='pdf')
def export_type(plot, type='pdf', file_name='chart')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was telling def export(plot, export_type='pdf', file_name='chart').

def append_chart_type(type='pdf')
case type
when 'pdf'
"\n type: 'application/pdf',"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't tab is easy to read for indentation.

end
end # initialize context end

context '#export_type' do
it "should generate the valid script to export the chart" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better, if you write specs for each export type and check for saved file(png, pdf, ..)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method generates the JS code in String format (or loads it in IRuby) to download the chart. I have updated the documentation of it. It will export the chart when it will run in web frameworks or IRuby notebook. I will write the specs for each export type. Should I write them in the same way or is there any other way possible?

@Prakriti-nith
Copy link
Contributor Author

I just found that while exporting multiple highcharts simultaneously in web frameworks, it is exporting only the last one. I am trying to find a solution to resolve this issue.

@Prakriti-nith
Copy link
Contributor Author

By using exportChartLocal (useful link), not only the issue has been resolved but also the exporting works offline (export button in highcharts works online). In IRuby notebook, online exporting asks to leave the page and after that that jupyter notebook does not work (try exporting using exporting button) but this method (export) resolves all.

@Prakriti-nith
Copy link
Contributor Author

In IRuby notebook, offline-export supports only the exporting to png, jpeg and svg format. Export to PDF is not working (not even through the exporting button in highchart). Also, in IRuby notebook, online exporting asks to leave the page and after that that jupyter notebook does not work (try exporting using exporting button). Still, I am keeping the online exporting for the IRuby notebook so that at least chart can be exported to all the formats and offline-exporting for the web frameworks.

@@ -65,6 +65,10 @@ def export_html_file(plot, path='./plot.html')
File.write(path, str)
end

def export(plot, export_type='pdf', file_name='chart')
# TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should throw error saying not implemented yet.

js = ''
js << "\n <script>"
js << "\n (function() {"
js << "\n var onload = window.onload;"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it tab is better than space to understand the alignment?

@@ -10,8 +10,7 @@ def self.generate_init_code(dependent_js)
# Enable to show plots on IRuby notebook
def self.init_iruby(
dependent_js=['highstock.js', 'highcharts-more.js', 'modules/exporting.js',
'highcharts-3d.js', 'modules/data.js',
'modules/offline-exporting.js']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it will not work without internet ? Please document it.

# @param file_name [String] The name of the file after exporting the chart
# @return [String, void] js code of chart along with the code to export it
# and loads the js code to export it in IRuby.
def export(export_type='pdf', file_name='chart')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please write few examples in documentation.

@Shekharrajak
Copy link
Member

IRuby notebook example link ? Also along with documenting the method, write few example for user to understand the API.

@Prakriti-nith
Copy link
Contributor Author

I have commented out the export method in the examples here.
I also tried running it in console, it is generating the correct script to download the chart.

@Prakriti-nith Prakriti-nith force-pushed the export_highchart branch 2 times, most recently from 700f511 to 53d3845 Compare June 16, 2018 20:56
@Prakriti-nith
Copy link
Contributor Author

@Shekharrajak The tests of updating the js files runs sometimes perfectly and sometimes doesn't (maybe because the highcharts js doesn't get loaded fully and network error occurs). I don't understand the exact reason. Can you please check this?

@Prakriti-nith Prakriti-nith changed the title Added export_image method to export highchart in different formats Added export method to export highchart in different formats Jun 23, 2018
Added compatibility for IRuby notebook

Added file_name

Corrected alignment of lines on web page

Remove export_image_iruby from plot.rb

Updated method name to export

Updated documentation

Added more specs

Use exportChartLocal instead of exportChart

Online exporting in IRuby notebook

Added documentation

Updated examples accordingly

Change default type to png
@coveralls
Copy link

coveralls commented Jul 7, 2018

Pull Request Test Coverage Report for Build 602

  • 179 of 183 (97.81%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.6%) to 97.126%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/daru/view/adapters/googlecharts.rb 1 2 50.0%
lib/daru/view/adapters/nyaplot.rb 1 2 50.0%
lib/daru/view/adapters/highcharts/display.rb 48 50 96.0%
Totals Coverage Status
Change from base Build 601: 0.6%
Covered Lines: 1690
Relevant Lines: 1740

💛 - Coveralls

@coveralls
Copy link

Pull Request Test Coverage Report for Build 583

  • 181 of 183 (98.91%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 96.332%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/daru/view/adapters/googlecharts.rb 1 2 50.0%
lib/daru/view/adapters/nyaplot.rb 1 2 50.0%
Totals Coverage Status
Change from base Build 578: 0.2%
Covered Lines: 2127
Relevant Lines: 2208

💛 - Coveralls

2 similar comments
@coveralls
Copy link

Pull Request Test Coverage Report for Build 583

  • 181 of 183 (98.91%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 96.332%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/daru/view/adapters/googlecharts.rb 1 2 50.0%
lib/daru/view/adapters/nyaplot.rb 1 2 50.0%
Totals Coverage Status
Change from base Build 578: 0.2%
Covered Lines: 2127
Relevant Lines: 2208

💛 - Coveralls

@coveralls
Copy link

Pull Request Test Coverage Report for Build 583

  • 181 of 183 (98.91%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.2%) to 96.332%

Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/daru/view/adapters/googlecharts.rb 1 2 50.0%
lib/daru/view/adapters/nyaplot.rb 1 2 50.0%
Totals Coverage Status
Change from base Build 578: 0.2%
Covered Lines: 2127
Relevant Lines: 2208

💛 - Coveralls

@Prakriti-nith
Copy link
Contributor Author

@Shekharrajak can you please review the changes?

Copy link
Member

@Shekharrajak Shekharrajak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the usage in separate Wiki page as well.
It looks good to go after minor modification as per the comment. 🎊

@@ -70,6 +70,21 @@ def export_html_file(plot, path='./plot.html')
File.write(path, str)
end

# Expoting in web frameforks is completely offline. In IRuby notebook,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expoting -> Exporting
frameforks -> frameworks

# Expoting in web frameforks is completely offline. In IRuby notebook,
# offline-export supports only the exporting to png, jpeg and svg format.
# Export to PDF is not working (not even through the exporting button in
# highchart). So, online exporting is done in IRuby notebook. There is a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can open the issue to track it, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll open it after this PR gets merged.

@Prakriti-nith
Copy link
Contributor Author

@Shekharrajak I have documented the usage in this wiki page.

@Shekharrajak
Copy link
Member

@Prakriti-nith , please resolve the conflicts and run the examples once.

@Prakriti-nith
Copy link
Contributor Author

@Shekharrajak I tried the examples again both in IRuby notebook and rails, everything is working fine.

@Shekharrajak Shekharrajak merged commit 8d4f389 into SciRuby:master Jul 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants